mysql中left join,right join,inner join,outer join的用法详解【转】 您所在的位置:网站首页 mysql中 left join mysql中left join,right join,inner join,outer join的用法详解【转】

mysql中left join,right join,inner join,outer join的用法详解【转】

2023-10-31 03:52| 来源: 网络整理| 查看: 265

非常惭愧用了这么久的mysql居然没有用过outer join和inner join,对outer join的认识也仅是知道它是外连结,至于什么用途都不清楚,至于还有没有left outer join更是不得而知,某天有人问起,才想起自己mysql知识的贫乏,赶紧找了一下网上的left join,right join,inner join,outer join的用法来学习一下下面的内容转载自网络,你也可以查看这篇文章 http://blog.wdou.cn/index.php/7139/viewspace-14285.html下面是例子分析表A记录如下:aID aNum1 a200501112 a200501123 a200501134 a200501145 a20050115表B记录如下:bID bName1 20060324012 20060324023 20060324034 20060324048 2006032408创建这两个表SQL语句如下:CREATE TABLE aaID int( 1 ) AUTO_INCREMENT PRIMARY KEY ,aNum char( 20 ))CREATE TABLE b(bID int( 1 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,bName char( 20 ))INSERT INTO aVALUES ( 1, 'a20050111' ) , ( 2, 'a20050112' ) , ( 3, 'a20050113' ) , ( 4, 'a20050114' ) , ( 5, 'a20050115' ) ;INSERT INTO bVALUES ( 1, ' 2006032401' ) , ( 2, '2006032402' ) , ( 3, '2006032403' ) , ( 4, '2006032404' ) , ( 8, '2006032408' ) ;实验如下:1.left join(左联接)sql语句如下:SELECT * FROM aLEFT JOIN bON a.aID =b.bID结果如下:aID aNum bID bName1 a20050111 1 20060324012 a20050112 2 20060324023 a20050113 3 20060324034 a20050114 4 20060324045 a20050115 NULL NULL(所影响的行数为 5 行)结果说明:left join是以A表的记录为基础的,A可以看成左表,B可以看成右表,left join是以左表为准的.换句话说,左表(A)的记录将会全部表示出来,而右表(B)只会显示符合搜索条件的记录(例子中为: A.aID = B.bID).B表记录不足的地方均为NULL.2.right join(右联接)sql语句如下:SELECT * FROM aRIGHT JOING bON a.aID = b.bID结果如下:aID aNum bID bName1 a20050111 1 20060324012 a20050112 2 20060324023 a20050113 3 20060324034 a20050114 4 2006032404NULL NULL 8 2006032408(所影响的行数为 5 行)结果说明:仔细观察一下,就会发现,和left join的结果刚好相反,这次是以右表(B)为基础的,A表不足的地方用NULL填充.3.inner join(相等联接或内联接)sql语句如下:SELECT * FROM aINNER JOIN bON a.aID =b.bID等同于以下SQL句:SELECT *FROM a,bWHERE a.aID = b.bID结果如下:aID aNum bID bName1 a20050111 1 20060324012 a20050112 2 20060324023 a20050113 3 20060324034 a20050114 4 2006032404结果说明:很明显,这里只显示出了 A.aID = B.bID的记录.这说明inner join并不以谁为基础,它只显示符合条件的记录.LEFT JOIN操作用于在任何的 FROM 子句中,组合来源表的记录。使用 LEFT JOIN 运算来创建一个左边外部联接。左边外部联接将包含了从第一个(左边)开始的两个表中的全部记录,即使在第二个(右边)表中并没有相符值的记录。语法:FROM table1 LEFT JOIN table2 ON table1.field1 compopr table2.field2说明:table1, table2参数用于指定要将记录组合的表的名称。field1, field2参数指定被联接的字段的名称。且这些字段必须有相同的数据类型及包含相同类型的数据,但它们不需要有相同的名称。compopr参数指定关系比较运算符:"=", "", "=" 或 ""。如果在INNER JOIN操作中要联接包含Memo 数据类型或 OLE Object 数据类型数据的字段,将会发生错误。 MySQL中的各种JOIN

1. 笛卡尔积(交叉连接)在MySQL中可以为CROSS JOIN或者省略CROSS即JOIN,或者使用',' 如SELECT * FROM table1 CROSS JOIN table2 SELECT * FROM table1 JOIN table2SELECT * FROM table1,table2

由于其返回的结果为被连接的两个数据表的乘积,因此当有WHERE, ON或USING条件的时候一般不建议使用,因为当数据表项目太多的时候,会非常慢。一般使用LEFT [OUTER] JOIN或者RIGHT [OUTER] JOIN

2. 内连接INNER JOIN在MySQL中把INNER JOIN叫做等值连接,即需要指定等值连接条件在MySQL中CROSS和INNER JOIN被划分在一起,不明白。参看MySQL帮助手册http://dev.mysql.com/doc/refman/5.0/en/join.htmljoin_table:    table_reference [INNER | CROSS] JOIN table_factor [join_condition]

3. MySQL中的外连接,分为左外连接和右连接,即除了返回符合连接条件的结果之外,还要返回左表(左连接)或者右表(右连接)中不符合连接条件的结果,相对应的使用NULL对应。

a. LEFT [OUTER] JOIN SELECT column_name FROM table1 LEFT [OUTER] JOIN table2 ON table1.column=table2.column除了返回符合连接条件的结果之外,还需要显示左表中不符合连接条件的数据列,相对应使用NULL对应

b. RIGHT [OUTER] JOINSELECT column_name FROM table1 RIGHT [OUTER] JOIN table2 ON table1.column=table2.columnRIGHT与LEFT JOIN相似不同的仅仅是除了显示符合连接条件的结果之外,还需要显示右表中不符合连接条件的数据列,相应使用NULL对应

--------------------------------------------添加显示条件WHERE, ON, USING1. WHERE子句2. ON3. USING子句,如果连接的两个表连接条件的两个列具有相同的名字的话可以使用USING例如SELECT FROM LEFT JOIN USING ()

连接多余两个表的情况举例:mysql> SELECT  artists.Artist, cds.title, genres.genre     -> FROM cds    -> LEFT JOIN genres     -> ON cds.genreID = genres.genreID     -> LEFT JOIN artists     -> ON cds.artistID = artists.artistID;或者mysql> SELECT artists.Artist, cds.title, genres.genre     -> FROM cds    -> LEFT JOIN genres     -> ON cds.genreID = genres.genreID     -> LEFT JOIN artists     -> ON cds.artistID = artists.artistID     -> WHERE (genres.genre = 'Pop');--------------------------------------------

另外需要注意的地方

在MySQL中涉及到多表查询的时候,需要根据查询的情况,想好使用哪种连接方式效率更高。1. 交叉连接(笛卡尔积)或者内连接[INNER | CROSS] JOIN2. 左外连接LEFT [OUTER] JOIN或者右外连接RIGHT [OUTER] JOIN

注意指定连接条件WHERE, ON,USING.

--------------------------------------------看懂MySQL手册定义的MySQL各种JOIN的用法://看懂如下的定义方式

table_references:    table_reference [, table_reference] ...//不同的JOIN EXPRESSION之间使用','分割A table reference is also known as a join expression.table_reference:    table_factor  | join_table//每个JOIN EXPRESSION由数据表table_factor以及JOIN表达式构成join_table table_factor:    tbl_name [[AS] alias] [index_hint)]  | ( table_references )  | { OJ table_reference LEFT OUTER JOIN table_reference        ON conditional_expr }//数据表table_factor,注意其递归定义的table_referencesjoin_table:    table_reference [INNER | CROSS] JOIN table_factor [join_condition]  | table_reference STRAIGHT_JOIN table_factor  | table_reference STRAIGHT_JOIN table_factor ON condition  | table_reference LEFT [OUTER] JOIN table_reference join_condition  | table_reference NATURAL [LEFT [OUTER]] JOIN table_factor  | table_reference RIGHT [OUTER] JOIN table_reference join_condition  | table_reference NATURAL [RIGHT [OUTER]] JOIN table_factor//数据表的连接表达式join_table join_condition:    ON conditional_expr  | USING (column_list)//连接表达式的连接条件定义使用ON或者USING index_hint:    USE  [FOR JOIN] (index_list)  | IGNORE  [FOR JOIN] (index_list)  | FORCE  [FOR JOIN] (index_list)index_list:    index_name [, index_name] ...MySQL手册中提到的JOIN需要注意的地方:1. In MySQL, CROSS JOIN is a syntactic equivalent to INNER JOIN (they can replace each other). In standard SQL, they are not equivalent. INNER JOIN is used with an ON clause, CROSS JOIN is used otherwise.手册中提到标准SQL中CROSS JOIN交叉连接(笛卡尔积)和内连接INNER JOIN不同,但是MySQL中两者是相同的,即有[CROSS | INNER] JOIN,两者可以互相替代,而且可以只使用JOIN

2. A table reference can be aliased using tbl_name AS alias_name or tbl_name alias_name: SELECT t1.name, t2.salary  FROM employee AS t1 INNER JOIN info AS t2 ON t1.name = t2.name; 可以对数据表使用别名3. ,运算符例如SELECT * FROM table1,table2 由于在MySQL中INNER JOIN与CROSS JOIN相同,INNER JOIN和 , 在MySQL也相同,都是产生两个表的笛卡尔积Cartesian Product(等于两个表格的行数乘积)但是,号的优先级要低于INNER JOIN, CROSS JOIN, LEFT JOIN因此If you mix comma joins with the other join types when there is a join condition, an error of the form. Unknown column 'col_name' in 'on clause' may occur.4. 什么时候使用ON,什么时候使用WHEREON应该用户数据表连接的时候指定连接条件;WHERE用于用户限制所选取的列例如ON a.column=b.columnWHERE a.column='hello'5. 可以使用LEFT JOIN查看,两个连接的表中,不符合连接条件的部分,因为不符合条件的部分LEFT JOIN之后会显示为NULL  If there is no matching row for the right table in the ON or USING part in a LEFT JOIN, a row with all columns set to NULL is used for the right table. You can use this fact to find rows in a table that have no counterpart in another table:SELECT left_tbl.*  FROM left_tbl LEFT JOIN right_tbl ON left_tbl.id = right_tbl.id  WHERE right_tbl.id IS NULL;This example finds all rows in left_tbl with an id value that is not present in right_tbl (that is, all rows in left_tbl with no corresponding row in right_tbl). This assumes that right_tbl.id is declared NOT NULL. 6.当别连接的表指定连接条件的列举有相同的名称的时候,不需要ON a.column=b.column不同的时候才使用ON a.column_a=b.column_b可以使用USING (column)当然也可以使用多个USING (c1,c2,c3)The USING(column_list) clause names a list of columns that must exist in both tables. If tables a and b both contain columns c1, c2, and c3, the following join compares corresponding columns from the two tables:a LEFT JOIN b USING (c1,c2,c3)7.其他的:#The NATURAL [LEFT] JOIN of two tables is defined to be semantically equivalent to an INNER JOIN or a LEFT JOIN with a USING clause that names all columns that exist in both tables.#RIGHT JOIN works analogously to LEFT JOIN. To keep code portable across databases, it is recommended that you use LEFT JOIN instead of RIGHT JOIN.#The { OJ ... LEFT OUTER JOIN ...} syntax shown in the join syntax description exists only for compatibility with ODBC. The curly braces in the syntax should be written literally; they are not metasyntax as used elsewhere in syntax descriptions.#STRAIGHT_JOIN is similar to JOIN, except that the left table is always read before the right table. This can be used for those (few) cases for which the join optimizer puts the tables in the wrong order. 参考资料http://www.w3schools.com/sql/sql_join.asphttp://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php http://dev.mysql.com/doc/refman/5.0/en/join.html文章来自:老李的日志。源地址:http://www.dayanmei.com/blog.php/ID_1008.htm



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有